﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Common;
using Business;

namespace WinUI.Frm
{
    public partial class LoginFrm : DevExpress.XtraEditors.XtraForm
    {
        public LoginFrm()
        {
            InitializeComponent();
        }

        private void LoginFrm_Load(object sender, EventArgs e)
        {
            txtUserName.Text = "admin";
            txtPassword.Text = "123";
        }

        private bool ValidateInput()
        {
            return (!String.IsNullOrEmpty(txtUserName.Text) && !String.IsNullOrEmpty(txtPassword.Text));
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (ValidateInput())
            {
                string username = txtUserName.Text;
                string password = txtPassword.Text;
                string messageSystemError = String.Empty;
                //string encryptPassword = Common.EncryptionUtil.Encode(password, username);
                Business.User user = new Business.User();
                Common.LoginInfo loginInfo = new Common.LoginInfo(username, password);
                loginInfo = user.LoginWeb(ref messageSystemError, loginInfo);
                if (loginInfo.LoginResult == Common.ERROR_CODE.ERROR_CODE_LOGIN.SUCCESS)
                {
                    Global._UserInfo = loginInfo.UserInfo;
                    Global.InitApp = true;
                    this.Close();
                }
                else
                {
                    lbLoginError.Visible = true;
                    lbLoginError.Text = Common.EnumUtils.stringValueOf(loginInfo.LoginResult) + " " + messageSystemError;
                }
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Global.InitApp = false;
            Application.ExitThread();
            Application.Exit();
        }
    }
}